<?php

/**
 * CategoryController is the default controller for this application
 */
class CategoryController extends BrowseControllerBase {
	/*
	 * SystemCategoryDataService Object
	 */
	public $systemCategoryDataService;
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		//Check if User is logged in
		if ($this->authentication->getCurrentUserId () == null) {
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$type = $this->getQuery ( 'type', '' );
		$type .= "_category";
		$module = "";
		switch ($type) {
			case CATEGORY_BLOG :
				$module = MODULE_BLOGGING;
				break;
			case CATEGORY_NEWS :
				$module = MODULE_NEWS;
				break;
			case CATEGORY_PHOTO :
				$module = MODULE_GALLERY;
				break;
			case CATEGORY_PRODUCT :
				$module = MODULE_ECOMMERCE;
				break;
		}
		
		if ($this->currentPackageOption && ! empty ( $module )) {
			$isEnabled = $this->currentPackageOption->isEnabled ( $module );
			if ($isEnabled == false) {
				$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Index/errorAuthor';
				CommonUtils::redirect ( $url );
				return - 1;
			}
		}
		
		// Init all DataService necessary
		$this->systemCategoryDataService = new SystemCategoryDataService ();
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		$authentication = new Authentication ();
		
		$type = $this->getQuery ( "type", "" );
		// list page
		$listCategory = array (CATEGORY_BLOG, CATEGORY_NEWS, CATEGORY_PHOTO, CATEGORY_PRODUCT );
		$this->smarty->assign ( 'type', $type );
		$type .= "_category";
		if (! in_array ( $type, $listCategory )) {
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/MyAccount/index';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$category_url = "";
		$category_text = "";
		$parent_category_url = "";
		$parent_category_text = "";
		
		switch ($type) {
			case CATEGORY_BLOG :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/create/type/blog';
				$category_text = "Create A Blog Category";
				$parent_category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/BlogPanel/index';
				$parent_category_text = "Blogging Panel";
				break;
			case CATEGORY_NEWS :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/create/type/news';
				$category_text = "Create A News Category";
				$parent_category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/NewsPanel/index';
				$parent_category_text = "News Panel";
				break;
			case CATEGORY_PHOTO :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/create/type/photo';
				$category_text = "Create A Photo Category";
				$parent_category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/GalleryPanel/index';
				$parent_category_text = "Gallery Panel";
				break;
			case CATEGORY_PRODUCT :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/create/type/product';
				$category_text = "Create A Product Category";
				$parent_category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/EcommercePanel/index';
				$parent_category_text = "ECommerce Panel";
				break;
		
		}
		
		$arrElement = array ();
		$arrElement ['ownerId'] = $authentication->getCurrentUserId ();
		$arrElement ['type'] = $type;
		
		$itemList = $this->systemCategoryDataService->getItems ( $arrElement );
		
		$this->smarty->assign ( 'itemList', $itemList );
		$this->smarty->assign ( 'itemCount', count ( $itemList ) );
		
		$this->smarty->assign ( 'category_url', $category_url );
		$this->smarty->assign ( 'category_text', $category_text );
		$this->smarty->assign ( 'parent_category_url', $parent_category_url );
		$this->smarty->assign ( 'parent_category_text', $parent_category_text );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Category/list_category.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function createAction() {
		
		$authentication = new Authentication ();
		
		$type = $this->getQuery ( "type", "" );
		if (empty ( $type )) {
			$type = $this->getPost ( "type", "" );
		}
		// list page
		$listCategory = array (CATEGORY_BLOG, CATEGORY_NEWS, CATEGORY_PHOTO, CATEGORY_PRODUCT );
		
		$this->smarty->assign ( 'type', $type );
		$tmpType = $type;
		
		$type .= "_category";
		if (! in_array ( $type, $listCategory )) {
			// Go on to login page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/MyAccount/index';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		$category_url = "";
		$category_text = "";
		
		switch ($type) {
			case CATEGORY_BLOG :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/blog';
				$category_text = "Blog Category Panel";
				
				break;
			case CATEGORY_NEWS :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/news';
				$category_text = "News Category Panel";
				
				break;
			case CATEGORY_PHOTO :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/photo';
				$category_text = "Photo Category Panel";
				
				break;
			case CATEGORY_PRODUCT :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/product';
				$category_text = "Product Category Panel";
				
				break;
		
		}
		
		$this->smarty->assign ( 'category_url', $category_url );
		$this->smarty->assign ( 'category_text', $category_text );
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			// Get name field
			$name = $this->getPost ( 'name', "" );
			$name = trim ( $name );
			
			// check
			if (! empty ( $name )) {
				$authentication = new Authentication ();
				try {
					// Save data into database
					$category = $this->systemCategoryDataService->beforeCreate ();
					$category->name = $name;
					$category->type = $type;
					$category->ownerId = $authentication->getCurrentUserId ();
					$this->systemCategoryDataService->create ( $category );
					
					// Redirect to edit page
					$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/' . $tmpType;
					CommonUtils::redirect ( $url );
					exit ();
				} catch ( Exception $ex ) {
					$isError = true;
					// Ignore to process this case. Only show a message
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
				}
			}
		}
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Category/create_category.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function editAction() {
		
		// Get id of page
		$id = $this->getPost ( 'id' );
		if (empty ( $id )) {
			$id = $this->getQuery ( 'id' );
		}
		// If the id of page does not exist, go on to view list page
		if (empty ( $id ) || ! is_numeric ( $id )) {
			$this->indexAction ();
			return false;
		}
		
		$id = intval ( $id );
		
		// Get data
		$category = $this->systemCategoryDataService->getById ( $id );
		
		// check data
		if (empty ( $category )) {
			$this->indexAction ();
			return false;
		}
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		
		// submit
		if (! empty ( $actionForm ) && $actionForm == 'submit') {
			// Get name field
			$name = $this->getPost ( 'name', "" );
			$name = trim ( $name );
			
			$category->name = $name;
			
			// check
			if (! empty ( $name )) {
				try {
					$res = $this->systemCategoryDataService->update ( $category );
				} catch ( Exception $ex ) {
					
					// Ignore to process this case. Only show a message
					$error = 'The item cannot create successfully. Error Code = ' . $ex->getCode ();
					$isError = true;
				}
			} else {
				$isError = true;
				
				$error = 'Please fill in all required fields';
			}
		}
		
		$category_url = "";
		$category_text = "";
		$type = $category->type;
		
		switch ($type) {
			case CATEGORY_BLOG :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/blog';
				$category_text = "Blog Category Panel";
				
				break;
			case CATEGORY_NEWS :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/news';
				$category_text = "News Category Panel";
				
				break;
			case CATEGORY_PHOTO :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/photo';
				$category_text = "Photo Category Panel";
				
				break;
			case CATEGORY_PRODUCT :
				$category_url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/product';
				$category_text = "Product Category Panel";
				
				break;
		
		}
		
		$this->smarty->assign ( 'category_url', $category_url );
		$this->smarty->assign ( 'category_text', $category_text );
		
		$this->smarty->assign ( 'id', $category->id );
		$this->smarty->assign ( 'name', $category->name );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/Category/edit_category.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	public function delAction() {
		// Get Id of page
		$id = $this->getQuery ( 'id', 0 );
		
		if (! empty ( $id ) && is_numeric ( $id ) && $this->isDemoMode == false) {
			// Handle to get Type of Category
			$item = $this->systemCategoryDataService->getById ( $id );
			if (! empty ( $item )) {
				$type = $item->type;
				$arr = explode ( '_', $type );
				
				if (count ( $arr ) > 0) {
					$type = $arr [0];
				}
			}
			
			$id = intval ( $id );
			// Delete page
			$res = $this->systemCategoryDataService->del ( $id );
			
			// Redirect to edit page
			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Category/index/type/' . $type;
			CommonUtils::redirect ( $url );
			return;
		
		}
	}
}
